package site.action;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import site.config.CommonConfig;
import site.security.AesCryptor;
import site.security.HMacUtil;
import site.util.CookieUtils;
import site.util.Struts2Utils;

public class LoginAction extends ActionSupport {

    private String userName;
    private String passWord;

    private boolean flag;

    @Action(value = "/login", results = {@Result(name = "success", type = "freemarker", location = "/pages/login/Login.ftl")})
    public String init() throws Exception {
        return SUCCESS;
    }

    @Action(value = "/login/submit", results = {@Result(name = "success", type = "json", params = {"root",
            "flag"})})
    public String submit() throws Exception {
        passWord = HMacUtil.hmac(passWord);
        if (CommonConfig.USERNAME.equals(userName) && CommonConfig.PASSWORD.equals(passWord)) {
            flag = true;

            String encryptName = new AesCryptor().encrypt(userName);
            CookieUtils.setCookie(Struts2Utils.getResponse(), "UT", encryptName, 10 * 24 * 60 * 60);
            return SUCCESS;
        }
        flag = false;
        return SUCCESS;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }
}
